require('./cart.css');
import * as ajax from './ajax';



$('#balance').click(function () {
    let gids = getCheckId();
    let url = `${_orderIndexUrl}?gids=${gids}`;
    $(this).attr('href',url);
});

$('.check').iCheck({
    checkboxClass: 'iradio_square-blue'
});
$('.check').on('ifChanged', function () {
    updateTotalNums();
    updateTotalMoney(computeTotalMoney());
    if (isAllChecked()) {
        $('.checkall .iradio_square-blue').addClass('checked');
        $('.checkall input').prop('checked', true);

    } else {
        $('.checkall .checked').removeClass('checked');
        $('.checkall input').prop('checked', false);
    }
});



//全选按钮
$('.checkall').iCheck({
    checkboxClass: 'iradio_square-blue'
});

$('.checkall').on('ifChecked', function (e) {
    $('.check').iCheck('check');
});
$('.checkall').on('ifUnchecked', function (e) {
    $('.check').iCheck('uncheck');
});



//删除购物车商品
$('.good-list .del').click(function () {
    let gids = getCheckId();
    let id = $(this).parents('.good-list').find('.check input').val();
    ajax.delGood(id, gids, (res) => {
        if (res.rel == 1) {
            $(this).parents('.good-list').remove();
            if($('.good-list').length <= 0){
                $(".cartBox").html("<div class='no_goods'> </div>")
            }
            updateTotalNums();
            updateTotalMoney(res.cartsum);
        }
    });


});



//添加商品数量
$('.good-list .add').click(function (e) {
    let gids = getCheckId();
    let input = $(this).siblings('input');
    let id = input.data('gid');
    let curNum = parseInt(input.val());
    ajax.changeGoodNum(id, curNum + 1, gids, (res) => {
        if (res.rel == 1) {
            input.val(res.goodnum);
            updateTotalMoney(res.cartsum);
        } else {
            $.toast(res.msg);
        }
    });
});

//减少商品数量
$('.good-list .subtract').click(function (e) {
    let gids = getCheckId();
    let input = $(this).siblings('input');
    let id = input.data('gid');
    let curNum = parseInt(input.val());
    if (curNum > 1) {
        ajax.changeGoodNum(id, curNum - 1, gids, (res) => {
            if (res.rel == 1) {
                input.val(res.goodnum);
                updateTotalMoney(res.cartsum);
            } else {
                $.toast(res.msg);
            }
        });
    }
});


//计算购物车总价
const computeTotalMoney = () => {

    let parentDOM = $('.good-list input:checked').parents('.good-list');

    let priceAry = parentDOM.map(function () {
        let perPrice = parseFloat($(this).find('.price b').text()).toFixed(2);
        let num = parseInt($(this).find('.num input').val());
        return (perPrice * num);
    }).get();


    let sum = 0;
    priceAry.forEach(val => {
        sum += parseFloat(val);
    });

    return sum.toFixed(2);

};




//更新购物车总价
const updateTotalMoney = (money) => {
    $('.total-money .money').text(money);
}

//更新结算件数
const updateTotalNums = () => {
    let checkLength = $('.good-list input:checked').length;

    $('.balance span').text(checkLength);
};



//全选勾选状态
const isAllChecked = () => {

    let allLength = $('.good-list').get().length;
    let checkLength = $('.good-list input:checked').length;

    if (checkLength < allLength) {
        return false;
    }
    return true;
};






// 获取勾选商品id
const getCheckId = () => {

    let ids = $('.good-list .checked input').map(function () {
        return $(this).val();
    }).get().join(',');

    return ids;
};